#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
void Swap(char* p1, char* p2, int width)
{
	int i = 0;
	for (i = 0; i < width; i++)
	{
		char tmp = *p1;
		*p1 = *p2;
		*p2 = tmp;
		p1++;
		p2++;
	}
}
void my_qsort(void* base, int sz, int width, int (*cmp)(const void*, const void*))
{
	int i = 0;
	for (i = 0; i < sz-1; i++)
	{
		int j = 0;
		for (j = 0; j < sz - 1 - i; j++)
		{
			if (cmp((char*)base+j*width,(char*)base+(j+1)*width) > 0)
			{
					Swap((char*)base + j * width, (char*)base + (j + 1) * width, width);
			}
		}
	}
}
int cmp_int(const void* e1, const void* e2)
{
	return *(int*)e1 - *(int*)e2;
}
void test_int()
{
	int arr[] = { 4,3,5,0,2,1 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	my_qsort(arr, sz, sizeof(arr[0]), cmp_int);
	int i = 0;
	for (i = 0; i < sz; i++)
	{
		printf("%d ", arr[i]);
	}
}
typedef struct stu
{
	char name[10];
	int age;
}stu;
int cmp_stu_name(const void*e1,const void*e2)
{
	return strcmp(((stu*)e1)->name, ((stu*)e2)->name);
}
void test_stu_name()
{
	stu arr[] = { {"laoda",17},{"laosan",12},{"laoer",16} };
	int sz = sizeof(arr) / sizeof(arr[0]);
	my_qsort(arr, sz, sizeof(arr[0]), cmp_stu_name);
	int i = 0;
	for (i = 0; i < sz; i++)
	{
		printf("%s %d\n", arr[i].name,arr[i].age);
	}
}
int main()
{
	//test_int();
	test_stu_name();
	return 0;
}